import { objDeepCopy,getDateString } from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	created() {
		this.getAllReturnList(this);
		//获取下拉框
		this.getReturnselect(this);
	},
	mounted() {
		
	},
	activated() {
		this.getAllReturnList(this);
	},
	methods: {
		//获取列表接口
		getAllReturnList(that,params,pageIndex = 1,pageSize = 10) {
			params = params || {businessType:40};
			this.$ajax({
				method: 'post',
				//url:this.apiTest+'/supplier/whWarehouse/list',
				url:this.apiTest+'/supplier/whWarehouse/list',
				data:{
					condition:params,
					pageIndex: pageIndex,
					pageSize: pageSize,
				}
			}).then((res)=>{
				that.Model.tableData = [];
				var data = res.data;
				if(data.status == 200){
					var obj = {};
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					var result = data.result.result;
					console.log(result)
					if(result){
						for (var i = 0; i < result.length; i++) {
							obj = objDeepCopy(result[i]);
							console.log(obj);
							that.Model.tableData.push(obj);
						}
					}
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//下拉框
		getReturnselect(that) {
			this.$ajax({
				method: 'get',
				url:this.apiTest+'/supplier/whWarehouse/getInOutDropBox?parentId=205&type=0',
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					var result = data.result;
					 console.log(result)
					this.Model.filterOptionData = result;
					let wareHouseStatusArr = [], wareHouseTypeArr = [],supplierArr = [], locationArr = [];
					for (let i in result.wareHouseStatus) {//退货单状态
						wareHouseStatusArr.push({ label: result.wareHouseStatus[i], value: i });
					};
					for (let i in result.wareHouseType) {//退货单类型
						wareHouseTypeArr.push({ label: result.wareHouseType[i], value: i });
					};
					for (let i in result.supplier) {//供应商
						supplierArr.push({ label: result.supplier[i], value: i });
					};
					for (let i in result.location) {//出库仓库
						locationArr.push({ label: result.location[i], value: i });
					};
					this.Model.filterOption.items[1].data = wareHouseStatusArr;//退货单状态
					this.Model.filterOption.items[2].data = wareHouseTypeArr;//退货单类型
					this.Model.filterOption.items[3].data = supplierArr;//供应商
					this.Model.filterOption.items[4].data = locationArr;//出库仓库
					this.Model.defaultFilterOption = _.cloneDeep(this.Model.filterOption);
				}else{
					that.tips(that,'warning',data.msg)
				}
			
			});
		},
		//操作——作废
		Tovoid(row,type) {
			var that = this;
			this.$confirm('作废该出库单后，则无法再对该单进行操作，是否确认'+type+'?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				this.$ajax({
					method: 'post',
					//url:this.apiTest+'/supplier/whWarehouse/list',
					url:this.apiTest+'/supplier/whWarehouse/purchaseReturnInvalid',
					data:{
						id:row.id,
						status:'60'
					}
				}).then((res)=>{
					var data = res.data;
					if(data.status == 200){
						that.tips(that,'success','作废成功');
						this.getAllReturnList(this);
					}else{
						that.tips(that,'warning',data.msg)
					}
				
				});
			}).catch(() => {
				that.$message({
					type: 'info',
					message: '已取消'+type
				}); 
			});	
		},
		//操作——出库
		ToOutstorage(row) {
			var that = this;
			this.$ajax({
				method: 'post',
				//url:this.apiTest+'/supplier/whWarehouse/list',
				url:this.apiTest+'/supplier/whWarehouse/addPurchaseReturn',
				data:{
					id:row.id,
					status:'40'
				}
			}).then((res)=>{
				var data = res.data;
				if(data.status == 200){
					that.tips(that,'success','操作成功');
					this.getAllReturnList(this);
				}else{
					that.tips(that,'warning',data.msg)
				}
			});
		},
		tableDetail(row,flag) {
			this.$router.push({
				path:'purchaseReturnLeaveDetail',
				query: {
					id:row.number,
					flag
				}
			});
		},
		//编辑
		Toedit(row) {
			this.$router.push({
				name: 'purchaseReturnLeaveEdit',
				query: {
					id:row.number,
				}
			})
		},
		//新建
		newPage(row) {
			this.$router.push({
				name: 'purchaseReturnLeaveNew',
			})
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		handleSelectionChange(val) {
			console.log(val);
		},
		handleCurrentChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllReturnList(this,searchFromData,val);
		},
		handleSizeChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllReturnList(this,searchFromData,undefined,val);
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showChild(val) {
			this.Model.searchFromData = {
				businessType:40,
				keyword:val.keyword,
				type:val.type,
				status:val.status,
				supplierInfoId:val.supplierInfoId,
				outWarehouseId:val.outWarehouseId
			}
			if(val.createTime){
				this.Model.searchFromData.createTimeStart = getDateString(val.createTime[0]);
				this.Model.searchFromData.createTimeEnd = getDateString(val.createTime[1]);
			}
			if(val.storageTime){
				this.Model.searchFromData.outSureTimeStart = getDateString(val.outSureTime[0]);
				this.Model.searchFromData.outSureTimeEnd = getDateString(val.outSureTime[1]);
			}
			console.log(this.Model.searchFromData)
		},
		toSearch(val) {
			var searchFromData = this.Model.searchFromData;
			this.getAllReturnList(this,searchFromData);
			console.log(searchFromData)
		},
		toClear(val) {
			this.Model.searchFromData = _.cloneDeep(this.Model.defaultSearchFromData);
			this.Model.filterOption = _.cloneDeep(this.Model.defaultFilterOption);
		},
		toExport(val) {
			console.log(val);
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	}
}
export default Controller;